import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class StudentSystem {
    public static void main(String[] args) {

        ArrayList<Student> stu = new ArrayList<>();

        loop: while (true) {
            System.out.println("-------------欢迎来到学生管理系统------------------");
            System.out.println("1.添加学生");
            System.out.println("2.查询学生");
            System.out.println("3.删除学生");
            System.out.println("4.修改学生");
            System.out.println("5.退出系统");
            System.out.println("请输入您的选择:");
            Scanner sc = new Scanner(System.in);
            String choose = sc.next();
            switch (choose){
                case "1" : {
                    addStudent(stu);
                    break;
                }
                case "2" :{
                   findStudent(stu);
                    break;
                }
                case "3" : {
                    deleteStudent(stu);
                    break;
                }
                case "4" :  {
                    modifyStudent(stu);
                    break;
                }
                case "5" : {
                    System.out.println("退出系统");
                    //break loop;
                    System.exit(0);//停止虚拟机运行
                }
                default:
                    System.out.println("没有这个选项");
            }
        }

    }

    //添加学生
    public static void addStudent(ArrayList<Student> stu){
        Student student = new Student();

        Scanner sc = null;
        String id = null;
        while (true) {
            System.out.println("请输入学生id:");
            sc = new Scanner(System.in);

            id = sc.next();
            if(contains(stu, id)){
                System.out.println("id已经存在，请重新录入！");
            }else break;
        }
        student.setId(id);

        System.out.println("请输入学生姓名:");
        String name = sc.next();
        student.setName(name);

        System.out.println("请输入学生年龄:");
        int age = sc.nextInt();
        student.setAge(age);

        System.out.println("请输入家庭住址:");
        String address = sc.next();
        student.setAddress(address);

        stu.add(student);
        System.out.println("该学生信息添加成功！");
    }

    //修改学生
    public static void modifyStudent(ArrayList<Student> stu){
        System.out.println("请输入要修改的学生id");
         Scanner sc = new Scanner(System.in);
        String id = sc.next();
        int index = getIndex(stu,id);
        if(index == -1){
            System.out.println("id不存在，返回到初始菜单！");
            return;
        }

        //
        Student student = stu.get(index);
        System.out.println("请输入要修改的学生姓名:");
        String newName = sc.next();
        student.setName(newName);
        System.out.println("请输入要修改的学生年龄:");
        int newAge = sc.nextInt();
        student.setAge(newAge);
        System.out.println("请输入要修改的学生联系地址:");
        String newAddress= sc.next();
        student.setAddress(newAddress);

        System.out.println("学生信息修改成功！");

    }

    //查询学生
    public static void findStudent(ArrayList<Student> stu){

        //如果没有学生信息，提示当前无学生信息，请添加后再查询
        if(stu.size() == 0){
            System.out.println("当前无学生信息，请添加后再查询");
            return;
        }


        //打印学生信息
        System.out.println("id\t姓名\t年龄\t家庭住址");

        for (int i = 0; i < stu.size(); i++) {
            System.out.println(stu.get(i).getId() + "\t" + stu.get(i).getName() + "\t" + stu.get(i).getAge()
            + "\t" +stu.get(i).getAddress());
        }
    }

    //删除学生
    public static void deleteStudent(ArrayList<Student> stu){

        System.out.println("请输入要删除的学生id:");
        Scanner sc = new Scanner(System.in);
        String id = sc.next();

        //1.id存在删除
        //2.id不存在，需要提示不存在，并返回初始菜单
        int index = getIndex(stu,id);
        if(index >= 0){
            stu.remove(index);
            System.out.println("id:" + id + "删除成功！");
        }
        else{
            System.out.println("id不存在,返回到初始菜单。");
        }

    }

    //判断id在学生集合中是否存在
    public static boolean contains(ArrayList<Student> stu, String id){

//        for (int i = 0; i < stu.size(); i++) {
//            String tmp = stu.get(i).getId();
//            if(tmp.equals(id)){
//                return true;
//            }
//        }
//        //不存在返回false
//        return false;
        return getIndex(stu, id) >= 0;
    }

    //获取id在学生集合中的索引
    public static int getIndex(ArrayList<Student> stu, String id){
        for (int i = 0; i < stu.size(); i++) {
            String sid = stu.get(i).getId();
            if(sid.equals(id)){
                return i;
            }
        }

        return -1;
    }
}
